//
//  YMAccountAuthorizationVC.m
//  YMMedical
//
//  Created by y s on 2023/7/26.
//

#import "YMAccountAuthorizationVC.h"
#import "YMAccountAuthorizationListCell.h"
#import "YMHomeSectionHeadView.h"
#import "YMMineService.h"


@interface YMAccountAuthorizationVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *imgvTop;
@property (nonatomic, strong) UITableView *tvMain;

@property (nonatomic, strong) NSMutableArray *arrList;

@property (nonatomic, assign) NSInteger pageNo;

@end

@implementation YMAccountAuthorizationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self loadData:YES];
}

- (void)setUI{
    [self showNavTitle:KLanguage(@"auth_account_txt")];

    [self.view addSubview:self.imgvTop];
    [self.view addSubview:self.tvMain];
    
    [self.imgvTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo([SFitUtil fit:50]);
        make.size.mas_equalTo([SFitUtil fitSize:120 height:120]);
    }];
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(self.imgvTop.mas_bottom).offset([SFitUtil fit:50]);
    }];
    WEAK_SELF;
    [SThemeUtil refershHead:self.tvMain block:^{
        [weakSelf loadData:YES];
    }];
    [SThemeUtil refershFooter:self.tvMain block:^{
        [weakSelf loadData:NO];
    }];
}

- (void)loadData:(BOOL)isRefresh{
    if (isRefresh){
        _pageNo = 1;
    }
    WEAK_SELF;
    [YMMineService requestMineDeviceBindAccountListDataWithPage:_pageNo needpage:1 snCode:_snCode success:^(NSArray * _Nonnull arrData) {
        [weakSelf.tvMain.mj_header endRefreshing];
        if (isRefresh){
            [weakSelf.arrList removeAllObjects];
            [weakSelf.tvMain.mj_footer endRefreshing];
        }else{
            if (arrData.count > 0){
                [weakSelf.tvMain.mj_footer endRefreshing];
            }else{
                [weakSelf.tvMain.mj_footer endRefreshingWithNoMoreData];
            }
        }
        if (arrData.count > 0){
            [weakSelf.arrList addObjectsFromArray:arrData];
            weakSelf.pageNo++;
        }
        [weakSelf.tvMain reloadData];
    } failure:^(NSString * _Nonnull message) {
        [weakSelf.tvMain.mj_header endRefreshing];
        [weakSelf.tvMain.mj_footer endRefreshing];
        [SHUD showInfo:message];
    }];
}


- (void)deleteClickAction:(NSIndexPath *)indexPath{
    //解绑
    NSDictionary *dict = [SDataUtil dictOf:self.arrList[indexPath.section]];
    NSString *rowId = [SDataUtil stringOf:dict[@"id"]];
    WEAK_SELF;
    [YMMineService requestMineDeleteDeviceWithRowId:rowId success:^{
        [weakSelf.arrList removeObjectAtIndex:indexPath.section];  //删除数组里的数据
        [weakSelf.tvMain deleteRow:0 inSection:indexPath.section withRowAnimation:UITableViewRowAnimationLeft];
        [weakSelf.tvMain reloadData];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
}

#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.arrList.count > 0){
        return self.arrList.count;
    }else{
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YMAccountAuthorizationListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMAccountAuthorizationListCell" forIndexPath:indexPath];
    WEAK_SELF;
    cell.deleteUserBlock = ^{
        if (indexPath.section < weakSelf.arrList.count){
            [weakSelf deleteClickAction:indexPath];
        }
    };
    if (indexPath.section < self.arrList.count){
        cell.dict = [SDataUtil dictOf:self.arrList[indexPath.section]];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0){
        YMHomeSectionHeadView *view = [[YMHomeSectionHeadView alloc] init];
        view.strTitle = KLanguage(@"binded_account_txt");
        return view;
    }
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [YMAccountAuthorizationListCell getCellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return [SFitUtil fit:45];
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return [SFitUtil fit:15];
}

#pragma mark -- setter and getter
- (UIImageView *)imgvTop{
    if (_imgvTop == nil){
        _imgvTop = [[UIImageView alloc] init];
        _imgvTop.image = [UIImage imageNamed:@"mine_device_list_icon"];
    }
    return _imgvTop;
}
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = NO;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_tvMain registerClass:[YMAccountAuthorizationListCell class] forCellReuseIdentifier:@"YMAccountAuthorizationListCell"];
        
    }
    return _tvMain;
}
- (NSMutableArray *)arrList{
    if (_arrList == nil){
        _arrList = [[NSMutableArray alloc] init];
    }
    return _arrList;
}
@end
